package com.ellricky.baselib.ui.base;

import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.andr.common.ui.fragment.BaseToolBarFragment;
import com.andr.common.ui.mvp.BasePresenter;
import com.ellricky.baselib.R;
import com.ellricky.baselib.widget.TabVpView;
import com.google.android.material.tabs.TabLayout;

import java.util.List;

public abstract class BaseTabVpFragment extends BaseToolBarFragment<Void, BasePresenter<Void>> {

    protected TabVpView tabVp;
    protected LinearLayout llCommon;

    @Override
    public int getContentLayout() {
        return R.layout.base_layout_tab_vp;
    }


    @Override
    protected void initUIView(View root) {
        super.initUIView(root);
        mTvTitle.setText(getTitleRes());

        tabVp = root.findViewById(R.id.tabVp);
        llCommon = root.findViewById(R.id.ll_common);
        List<Fragment> fragments = getFragmentList();

        List<String> tList = getTabTitleList();

        tabVp.setUp(getChildFragmentManager(), fragments, tList);
        tabVp.getTabLayout().addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                onTabSelect(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        try {
            View view = customView();
            View searchView = searchView();
            view.setVisibility(View.GONE);
            if (searchView!=null){
                llCommon.addView(searchView);
            }
            if (view != null) {
                llCommon.addView(view);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public LinearLayout getCustomParentLayout() {
        return llCommon;
    }

    public void onTabSelect(int position) {

    }

    public View customView() {
        return null;
    }

    public View searchView() {
        return null;
    }

    @NonNull
    public abstract List<Fragment> getFragmentList();

    @NonNull
    public abstract List<String> getTabTitleList();

    public abstract int getTitleRes();

}
